package com.faya.green.service.impl;

import com.faya.green.abnormal.ServiceException;
import com.faya.green.customize.MyRealm;
import com.faya.green.dao.authority.AuthorityDao;
import com.faya.green.dao.sys.SysLogDao;
import com.faya.green.enumeration.StatusEnum;
import com.faya.green.enumeration.SysLogEnum;
import com.faya.green.model.Authority;
import com.faya.green.model.SysLog;
import com.faya.green.model.User;
import com.faya.green.model.web.UserRoleAuthor;
import com.faya.green.service.AuthorityService;

import com.faya.green.utils.CommonUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AuthorityServiceImpl implements AuthorityService {

	//压制全部警告
	@SuppressWarnings("all")
	@Autowired
	private AuthorityDao authorityDao;
	//压制全部警告
	@SuppressWarnings("all")
	@Autowired
	private SysLogDao sysLogDao;


	@Override
	public Authority getAuthorityById(String id) {
		return authorityDao.getAuthorityById(id);
	}



	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateAuthority(Authority authority) {
		Authority sqlAuthority = authorityDao.getAuthorityById(authority.getId());
		if (sqlAuthority == null){
			throw new ServiceException("找不到待更新的权限数据");
		}

		authority.setUpdateTime(new Date());
		authorityDao.updateAuthority(authority);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void addAuthority(Authority authority) {
		if (authority == null){
			throw new ServiceException("权限不能为空");
		}
		authority.setId(UUID.randomUUID().toString());
		authority.setCreateTime(new Date());
		authority.setUpdateTime(new Date());
		if (CommonUtils.checkNull(authority.getStatus())){
			authority.setStatus(StatusEnum.STATUS_ADD.getCode());
		}

		authorityDao.addAuthority(authority);
	}

	@Override
	public List<Authority> getAuthorList() {
		return authorityDao.getAuthorList();
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void addOrUpdAuthority(Authority authority) {
		if (authority == null){
			throw new ServiceException("请填写权限后再执行操作");
		}

		if (CommonUtils.checkNull(authority.getId())){
			addAuthority(authority);
		} else {
			updateAuthority(authority);
		}
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void delAuthor(String authorId) {
		if (CommonUtils.checkNull(authorId)){
			throw new ServiceException("权限ID不能为空");
		}

		authorityDao.delAuthor(authorId);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void delAuthorLogic(String authorId) {
		if (CommonUtils.checkNull(authorId)){
			throw new ServiceException("权限ID不能为空");
		}

		Authority authority = authorityDao.getAuthorityById(authorId);
		if (authority == null){
			throw new ServiceException("未找到对应权限，请检查");
		}

		authority.setStatus(StatusEnum.STATUS_DELETE.getCode());
		authorityDao.updateAuthority(authority);

		//对缓存进行处理
		RealmSecurityManager rsm = (RealmSecurityManager)SecurityUtils.getSecurityManager();
		MyRealm myRealm = (MyRealm)rsm.getRealms().iterator().next();
		myRealm.clearCached();

		//日志记录
		SysLog sysLog = new SysLog();
		sysLog.setId(UUID.randomUUID().toString());
		sysLog.setType(SysLogEnum.TYPE_DEL.getCode());
		Subject subject = SecurityUtils.getSubject();
		User user = (User)subject.getPrincipal();
		sysLog.setCount("敏感操作：用户：" + user.getName() + "执行权限删除操作成功");
		sysLog.setCreateTime(CommonUtils.getTimestamp());
		sysLogDao.addSysLog(sysLog);
	}

	@Override
	public List<Authority> getAuthorByRoleId(String roleId) {
		return authorityDao.getAuthorByRoleId(roleId);
	}

	@Override
	public List<UserRoleAuthor> getUserRoleAuthorByUid(String userId) {
		return authorityDao.getUserRoleAuthorByUid(userId);
	}


}